//时间戳转化成时间格式
export  function formatTime(date){
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
  
    return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  }
  //时间戳转化成时间格式
  export  function formatTime1(date){
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
  
    return [year, month, day].map(formatNumber).join('-')
  }
  //时间戳转化成时间格式
  export  function now_formatTime1(){
    const date=new Date()
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()<10?'0'+date.getDate():date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()
  
    return [year, month, day].map(formatNumber).join('-')
  }
  
  const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
  }
  //获取从今天开始的前三天的日期  eg:20180602
  export function getThreeDaysBeforeDate (){
    var date =  new Date(), timestamp, newDate;
    timestamp = date.getTime();
    // 获取三天前的日期
    newDate = new Date(timestamp + 10 * 24 * 3600 * 1000);
    var year = newDate.getFullYear();
    // 月+1是因为js中月份是按0开始的
    var month = newDate.getMonth() + 1;
    var day = newDate.getDate();
    if (day < 10) { // 如果日小于10，前面拼接0
      day = '0' + day;
    }
    if (month < 10) { // 如果月小于10，前面拼接0
      month = '0' + month;
    }
    console.log([year, month, day].join(''))
    // return [year, month, day].join('');
  
  }
  
  // 从本月起的前5个月  eg:["2018-05", "2018-04", "2018-03", "2018-02", "2018-01", "2017-12"]
  export function last_year_month () {
    var d = new Date();
    var year=d.getFullYear()
    var result = [];
    for(var i = 0; i < 6; i++) {
      d.setMonth(d.getMonth() -1);
      var m = d.getMonth() + 2;
      if(m=='13'){
        year=d.getFullYear()+1;
        m=1
      }else{
        year=d.getFullYear();
      }
      m = m < 10 ? "0" + m : m;
      //在这里可以自定义输出的日期格式
      result.push(year + "-" + m);
      // result.push(d.getFullYear() + "年" + m + '月');
    }
    console.log(result)
    // return result;
  }
  
  //本周的周天到周六之间的日期   target('2018, 5, 23')  [20, 21, 22, 23, 24, 25, 26]
  export   function getWeek(target) {
    let now = new Date(target);
    let now_day = now.getDay();
    let now_time = now.getTime();
    let result = [0, 1, 2, 3, 4, 5, 6]
    return result.map(i => (new Date(now_time + 24 * 60 * 60 * 1000 * (i - now_day))).getDate())
  }
  //本周的星期一
    export function getMonDate  (){
      var d=new Date(),
        day=d.getDay(),
        date=d.getDate();
      if(day==1)
        return d;
      if(day==0)
        d.setDate(date-6);
      else
        d.setDate(date-day+1);
      return d;
    }
  
  // 0-6转换成中文名称
  export function getDayName (day)
  {
    var day=parseInt(day);
    if(isNaN(day) || day<0 || day>6)
      return false;
    var weekday=["星期天","星期一","星期二","星期三","星期四","星期五","星期六"];
    return weekday[day];
  }
  import {Toast} from 'mint-ui';
  //手机号校验
  export function validatemobile(mobile) {
    if (mobile.length == 0) {
      Toast({
        message: '请输入手机号',
        duration: 1500
      });
      return false;
    }
    if (mobile.length != 11) {
      Toast({
        title: '手机号长度有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(mobile)) {
      Toast({
        title: '手机号有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    return true;
  }
  
  export function getPosition () {
    return new Promise((resolve, reject) => {
      if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function (position) {
          let latitude = position.coords.latitude
          let longitude = position.coords.longitude
          let data = {
            latitude: latitude,
            longitude: longitude
          }
          resolve(data)
        }, function () {
          reject(arguments)
        })
      } else {
        reject('你的浏览器不支持当前地理位置信息获取')
      }
    })
  }
  